package com.shiliao.framework.util;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Build;

import com.shiliao.framework.FrameworkConfig;

import java.io.File;
import java.util.HashMap;
import java.util.Set;


public class FontManager {

    public final static String FONT_DEFAULT = "DEFAULT";
    public final static String FONT_APP_DEFAULT = "FZLTH";
    public final static String FONT_FZJT = "FZJT";
    public final static String FONT_FZKT = "FZKT";
    private Typeface mAppDefaultFace = null;

    private HashMap<String, String> mTypefaces = new HashMap<String, String>();
    public HashMap<String, Boolean> mMissfaces = new HashMap<String, Boolean>();

    private static FontManager instance = null;

    public static FontManager instance(Context appContext) {
        if (instance == null) {
            instance = new FontManager(appContext);
        }

        return instance;
    }

    public void scanFont(Context appContext) {
        int sdkVersion = Build.VERSION.SDK_INT;
        if (mAppDefaultFace == null) {
            mAppDefaultFace = Typeface.createFromAsset(appContext.getAssets(),
                    "fonts/syhtl.otf");
        }
        if (sdkVersion > 8) {
            try {
                mTypefaces.clear();
                File fontDir = new File(FrameworkConfig.FONT_FOLDER);
                String[] fonts = fontDir.list();
                if (fonts == null) return;
                updateTypefacesCache(FONT_DEFAULT, "");
                updateTypefacesCache(FONT_APP_DEFAULT, "");
                for (String font : fonts) {
                    if (mTypefaces.containsKey(font))
                        continue; // 用于更新字体映射表
                    String key = font.replace(".ttf", "").replace(".TTF", "").replace(".otf", "");
                    updateTypefacesCache(key, FrameworkConfig.FONT_FOLDER + "/" + font);
                    if (mMissfaces.containsKey(key))
                        mMissfaces.remove(key);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private void updateTypefacesCache(String fontname, String path) {
        mTypefaces.put(fontname, path);
    }

    private FontManager(Context appContext) {
        scanFont(appContext);
    }

    public String addNewFontFamily(String fontname, String ttfsrc) {
        try {
            updateTypefacesCache(fontname, ttfsrc);
            mMissfaces.remove(fontname);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return fontname;
    }

    // 应用初始化时进行维护
    public void addMissFontFamily(String fontName) {
        mMissfaces.put(fontName, true);
    }

    public Set<String> availableFontFamily() {
        if (mTypefaces != null) {
            return mTypefaces.keySet();
        }

        return null;
    }

    public Typeface loadTypeFace(String singleName) {
        if (mTypefaces.containsKey(singleName)) {
            try {
                if (singleName == FONT_DEFAULT)
                    return Typeface.DEFAULT;
                else if (singleName == FONT_APP_DEFAULT)
                    return mAppDefaultFace;
                else
                    return Typeface.createFromFile(mTypefaces.get(singleName));
            } catch (Exception e) {

            }
        }
        return null;
    }

    public Typeface getTypeFace(String fontFamily) {
        String[] fonts = fontFamily.split(",");
        Typeface tf = null;
        for (String font : fonts) {
            tf = loadTypeFace(font);
            if (tf != null)
                break;
        }
        if (tf == null) {
            LogUtil.w("Need:" + fontFamily + ",But:FZLTH");
            tf = mAppDefaultFace;
        }
        return tf;
    }

    public boolean hasFont(String fontFamily) {
        String[] fonts = fontFamily.split(",");
        for (String font : fonts) {
            if (mTypefaces.containsKey(font))
                return true;
        }
        return false;
    }

    public Typeface getDefaultFace() {
        return mAppDefaultFace;
    }
}
